//
//  QNShowToastView.m
//  QNReuseTool
//
//  Created by rsh on 2019/10/15.
//  Copyright © 2019年 rsh. All rights reserved.
//

#import "QNShowToastView.h"
#import <Masonry/Masonry.h>

// MARK: 控制器中弹出提示view

@interface QNShowToastView()<CAAnimationDelegate>


@property(nonatomic,strong) UIView *coverView;

@end

@implementation QNShowToastView

-(instancetype)init{
    self = [super init];
    if (self) {
        [self _setSubview];
    }
    return self;
}

-(void)_setSubview{
    self.userTouchActionEnable = YES;
    self.addHideAnimation = YES;
    self.addShowAnimation = YES;
    self.animationStyle = AnimationStyle_Detafult;
    self.hideAnimationStyle = HideAnimationStyle_Detafult;
    self.destoryWhenHideContentView = YES;
    self.layoutPositon = AutoLayoutPosition_center;
    [self addSubview:self.coverView];
}

-(void)setBackColor:(UIColor *)backColor{
    _backColor = backColor;
    self.coverView.backgroundColor = backColor;
}

-(void)showOnView:(UIView *)view{
    if (!self.contentView) {
        return;
    }
    
    [view addSubview:self];
    self.userInteractionEnabled = YES;
    self.frame = UIEdgeInsetsInsetRect(view.bounds, self.popViewInsets);
    self.coverView.frame = self.frame;
    
    
    self.coverView.alpha = 0.0;
    [self.layer removeAllAnimations];
    
    [self addSubview:self.contentView];
    
    if(self.useAutoLayout){
        self.contentView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addAutoLayoutContentView];
    }
    
    [self setNeedsLayout];
    [self layoutIfNeeded];
    
//    self.contentOriginFrame = self.contentView.frame;
    
    if (self.addShowAnimation) {
        [self showAnimation];
    }
    
    [UIView animateWithDuration:0.35 animations:^{
        self.coverView.alpha =  0.75;
    } completion:^(BOOL finished) {
        if (self.showCompletionBlock) {
            self.showCompletionBlock();
        }
    }];
}


-(void)showContentView:(UIView *)contentView fatherView:(UIView *)fatherView conmpletion:(void (^)())completionhandle{
    
    self.frame = fatherView.bounds;
    self.coverView.frame = fatherView.bounds;
    self.contentView = contentView;
    self.coverView.alpha = 0.0;
    [self.contentView.layer removeAllAnimations];
    
    [fatherView addSubview:self];
    [self addSubview:self.contentView];
    
    if(self.useAutoLayout){
        [self addAutoLayoutContentView];
    }
    
    if (self.addShowAnimation) {
        [self setNeedsLayout];
        [self layoutIfNeeded];
        
        [self showAnimation];
    }
    
    [UIView animateWithDuration:0.35 animations:^{
        self.coverView.alpha =  0.75;
    } completion:^(BOOL finished) {
        if (completionhandle) {
            completionhandle();
        }
    }];
    
}

#pragma mark addAutoLayoutContentView

-(void)addAutoLayoutContentView{
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        if (self.layoutPositon == AutoLayoutPosition_center) {
            make.centerX.centerY.equalTo(self);
            return ;
        }
        
        if (self.layoutPositon & AutoLayoutPosition_top) {
            make.top.equalTo(self);
            if (self.layoutPositon & AutoLayoutPosition_center) {
                make.centerX.equalTo(self);
            }
        }

        if (self.layoutPositon & AutoLayoutPosition_left) {
            make.left.equalTo(self);
            if (self.layoutPositon & AutoLayoutPosition_center) {
                make.centerY.equalTo(self);
            }
        }

        if (self.layoutPositon & AutoLayoutPosition_bottom) {
            make.bottom.equalTo(self);
            if (self.layoutPositon & AutoLayoutPosition_center) {
                make.centerX.equalTo(self);
            }
        }

        if (self.layoutPositon & AutoLayoutPosition_right) {
            make.right.equalTo(self);
            if (self.layoutPositon & AutoLayoutPosition_center) {
                make.centerY.equalTo(self);
            }
        }
        
        make.width.lessThanOrEqualTo(self.mas_width).offset(60);
        make.height.lessThanOrEqualTo(self.mas_height).offset(60);
    }];
    
   
}


-(void)showAnimation{
    switch (self.animationStyle) {
        case AnimationStyle_Detafult:{
            [self.contentView.layer addAnimation:[self defaultShowAnimation] forKey:@"scale-layer"];
        }
            break;
        case AnimationStyle_fromBottom:{
            [self showFromBottom];
        }
            break;
        case AnimationStyle_fromTop:{
            [self showFromTop];
        }
            break;
        case AnimationStyle_fromLeft:{
            [self showFromLeft];
        }
            break;
        case AnimationStyle_fromRight:{
            [self showFromRight];
        }
            break;
        default:
            break;
    }
}

-(CAAnimation *)defaultShowAnimation{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.duration = 0.25; // 动画持续时间
    animation.repeatCount = 1; // 重复次数
    animation.fromValue = [NSNumber numberWithFloat:0.001];
    animation.toValue = [NSNumber numberWithFloat:1.0];
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [animation setValue:@"show" forKey:@"AnimationKey"];
    return animation;
}


-(void)showFromLeft{
    CGRect frame = self.contentView.frame;
    CGRect frame1 = self.contentView.frame;
    frame.origin.x = -frame.size.width;
    self.contentView.frame = frame;
    
    [UIView animateWithDuration:0.35 animations:^{
        self.contentView.frame = frame1;
    } completion:^(BOOL finished) {
        
    }];
}

-(void)hideToLeft{
    CGRect frame = self.contentView.frame;
    frame.origin.x = -frame.size.width;
    
    [UIView animateWithDuration:0.35 animations:^{
        self.contentView.frame = frame;
    } completion:^(BOOL finished) {
        [self hide];
    }];
}

-(void)showFromRight{
    CGRect frame = self.contentView.frame;
    CGRect frame1 = self.contentView.frame;
    frame.origin.x = self.frame.size.width;
    self.contentView.frame = frame;
    
    [UIView animateWithDuration:0.35 animations:^{
        self.contentView.frame = frame1;
    } completion:^(BOOL finished) {
        
    }];
}

-(void)hideToRight{
    CGRect frame = self.contentView.frame;
    frame.origin.x = self.frame.size.width;
    
    [UIView animateWithDuration:0.35 animations:^{
        self.contentView.frame = frame;
    } completion:^(BOOL finished) {
        [self hide];
    }];
}

-(void)showFromTop{
    
    CGRect frame = self.contentView.frame;
    CGRect frame1 = self.contentView.frame;
    frame.origin.y = -frame.size.height;
    self.contentView.frame = frame;
    
    [UIView animateWithDuration:0.35 animations:^{
        self.contentView.frame = frame1;
    } completion:^(BOOL finished) {
        
    }];
}

-(void)hideToTop{
    
    CGRect frame = self.contentView.frame;
    frame.origin.y = -frame.size.height;
    
    [UIView animateWithDuration:0.35 animations:^{
        self.contentView.frame = frame;
    } completion:^(BOOL finished) {
        [self hide];
    }];
}

-(void)showFromBottom{
    
//    if(self.useAutoLayout){
//
//        return;
//    }
    
    CGRect frame = self.contentView.frame;
    frame.origin.y = self.frame.size.height;
    self.contentView.frame = frame;
    
    CGRect frame1 = self.contentView.frame;
    frame1.origin.y = self.frame.size.height - self.contentView.frame.size.height;
    [UIView animateWithDuration:0.35 animations:^{
        self.contentView.frame = frame1;
    } completion:^(BOOL finished) {
        
    }];
}

-(void)HideToBottom{
    CGRect frame = self.contentView.frame;
    frame.origin.y = self.frame.size.height;
    
    [UIView animateWithDuration:0.35 animations:^{
        self.contentView.frame = frame;
        self.coverView.alpha =  0.0;
    } completion:^(BOOL finished) {
        [self hide];
    }];
}

-(CAAnimation *)defaultHideAnimation{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    animation.duration = 0.25; // 动画持续时间
    animation.repeatCount = 1; // 重复次数
    animation.fromValue = [NSNumber numberWithFloat:1.0];
    animation.toValue = [NSNumber numberWithFloat:0.01];
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    animation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    animation.delegate = self;
    [animation setValue:@"hide" forKey:@"AnimationKey"];
    return animation;
}

-(void)hide{
    if (self.hideCompletionBlock) {
        self.hideCompletionBlock();
    }

    [self.contentView removeFromSuperview];
    [self removeFromSuperview];
    if (self.destoryWhenHideContentView) {
      self.contentView =  nil;
    }
    
}




#pragma mark CAAnimationDelegate
-(void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    if ([[anim valueForKey:@"AnimationKey"]isEqualToString:@"hide"]) {
       [self hide];
    }
}

-(void)animationDidStart:(CAAnimation *)anim{
    
}

-(void)hideContentView{
    if (self.addHideAnimation) {
        switch (self.hideAnimationStyle) {
            case HideAnimationStyle_Detafult:{
                [self.contentView.layer addAnimation:[self defaultHideAnimation] forKey:@"scale-layer-hide"];
            }
                break;
            case HideAnimationStyle_toBottom:{
                [self HideToBottom];
            }
                break;
            case HideAnimationStyle_toTop:{
                [self hideToTop];
            }
                break;
            case HideAnimationStyle_toLeft:{
                [self hideToLeft];
            }
                break;
            case HideAnimationStyle_toRight:{
                [self hideToRight];
            }
                break;
            default:
                break;
        }
    }else{
        [self hide];
    }
}

-(void)tapCoverView{
    if (self.userTouchActionEnable) {
        [self hideContentView];
    }
}

-(void)tapContentView{
    
}

#pragma mark

-(UIView *)coverView{
    if (!_coverView) {
        _coverView = [[UIView alloc]init];
        _coverView.backgroundColor = [UIColor blackColor];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapCoverView)];
        [_coverView addGestureRecognizer:tap];
    }
    return _coverView;
}


@end
